package com.FH.util;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 读取配置文件信息
 * 

@author 刘岳
@2016年1月13日13:03:02
 * */
public class XMLReader {

	/**
	 * 获取父节点下子节点内容
	 * 当前文件路径
	 * @return
	 * */
	public static String getXMLReader(String parentNode,String childNode){
		//构建返回对象
		List<String> result = new ArrayList<String>();
		try {
			//创建文件对象，读取文件内容
			File f = new File("/opt/wildfly-9.0.0.Final/bin/interface_address.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 

			//要获取节点的父节点名称
			NodeList nl = doc.getElementsByTagName(parentNode); 
			
			
			for(int i=0;i<nl.getLength();i++){
				result.add(doc.getElementsByTagName(childNode).item(i).getFirstChild().getNodeValue());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result.get(0);
		
	}
	
	/**
	 * 获取叶子节点内容
	 * 
	 * */
	public static List<String> getLeafXMLReader(String leafNode){
		//构建返回对象
		List<String> result = new ArrayList<String>();
		try {
			//创建文件对象，读取文件内容
			File f = new File("interface_address.xml"); 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder builder = factory.newDocumentBuilder(); 
			Document doc = builder.parse(f); 
			
			//要获取节点的父节点名称
			NodeList nl = doc.getElementsByTagName(leafNode); 
			
			for(int i=0;i<nl.getLength();i++){
				result.add(doc.getElementsByTagName(leafNode).item(i).getFirstChild().getNodeValue());
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
		
	}
	
	public static void main(String[] args) {
//		List<String> ss = XMLReader.getXMLReader("page/data_10k.xml", "VALUE","NO");
//		List<String> ss = XMLReader.getLeafXMLReader("NO");
//		System.out.println(ss.get(0));
//		System.out.println(ss.get(1));
		String ss1 = XMLReader.getXMLReader("organization_people","address");
		System.out.println(ss1);
	}
	
}
